﻿using ASPItalia.ModelVirtualCasting.Common;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.Unity.InterceptionExtension;

namespace ASPItalia.ModelVirtualCasting
{
    public sealed class ApplicationContext : IApplicationContext
    {
        private static object _sync = new object();
        private static volatile ApplicationContext _currentInstance;
        
        private ApplicationContext()
        {
            this.Initialize();
        }

        private void Initialize()
        {
            switch (this.RepositoryFactoryType)
            {
                case RepositoryFactoryType.Mef:
                    this.RepositoryFactory = new MefRepositoryFactory();
                    break;
                default:
                    this.RepositoryFactory = new UnityRepositoryFactory();
                    break;
            }
        }

        public static ApplicationContext Current
        {
            get
            {
                if (_currentInstance.IsNull())
                {
                    lock (_sync)
                    {
                        if (_currentInstance.IsNull())
                        {
                            _currentInstance = new ApplicationContext();
                        }
                    }
                }

                return _currentInstance;
            }
        }

        public IRepositoryFactory RepositoryFactory { get; private set; }

        public RepositoryFactoryType RepositoryFactoryType
        {
            get { return Configuration.RepositoryFactoryType; }
        }

        public IUnityContainer Container
        {
            get { return this.RepositoryFactory.Container as IUnityContainer; }
        }

        public ICandidateRepository Candidates
        {
            get { return this.RepositoryFactory.CreateCandidateRepository(); }
        }

        public ICountryRepository Countries
        {
            get { return this.RepositoryFactory.CreateCountryRepository(); }
        }

        public IJobLevelRepository JobLevels
        {
            get { return this.RepositoryFactory.CreateJobLevelRepository(); }
        }

        public IJobTypeRepository JobTypes
        {
            get { return this.RepositoryFactory.CreateJobTypeRepository(); }
        }

        public ILanguageRepository Languages
        {
            get { return this.RepositoryFactory.CreateLanguageRepository(); }
        }

        public IModelRepository Models
        {
            get { return this.RepositoryFactory.CreateModelRepository(); }
        }

        public INewsRepository News
        {
            get { return this.RepositoryFactory.CreateNewsRepository(); }
        }

        public IPersonRepository People
        {
            get { return this.RepositoryFactory.CreatePersonRepository(); }
        }

        public IRequestRepository Requests
        {
            get { return this.RepositoryFactory.CreateRequestRepository(); }
        }

        public IUserRepository Users
        {
            get { return this.RepositoryFactory.CreateUserRepository(); }
        }

        public IUserRoleRepository UserRoles
        {
            get { return this.RepositoryFactory.CreateUserRoleRepository(); }
        }
    }
}
